package cn.cdqf.manager.controller.validator;

import cn.cdqf.global.exception.GlobalException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;

import java.util.List;

public class BindingResultUtil {

    public static void  checkBindingResultUtil(BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            //获得所有参数校验的错误
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            //HashMap<String , String> objectObjectHashMap = Maps.newHashMap();
            //在方法里面是局部变量不会有线程问题
            StringBuilder stringBuilder = new StringBuilder();
            for(ObjectError allError:allErrors){
                FieldError fieldError = (FieldError) allError;
                stringBuilder.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append(";");
                //objectObjectHashMap.put(  fieldError.getField(),fieldError.getDefaultMessage());
            }
            //转成字符串
            //String errors = FastJsonTools.toJSONFeaturesString(objectObjectHashMap);

            throw new GlobalException(stringBuilder.toString());
        }
    }
}
